﻿Imports System
Imports System.Data
Imports ADOConnection
Imports System.Data.SqlClient
Imports System.Text

Public Class CPhieuNX
    Protected _dpnx_id As Integer
    Protected _nx As String
    Protected _loaimh As String
    Protected _sophieu As String
    Protected _ngayph As Date
    Protected _dvgn As String
    Protected _tcnx As String
    Protected _lk As String
    Protected _theolenh As String
    Protected _hopdong As Integer
    Protected _nguoign As String
    Protected _ggt As String
    Protected _ngaykggt As Date
    Protected _ngaygn As Date
    Protected _nguoivp As String
    Protected _ngay As Date
    Protected _sokh As Integer
    Protected _sokien As Integer
    Protected _trluong As Single
    Protected _httt As Integer
    Protected _tgiatri As Double
    Protected _bcth As Integer
    Protected _kyqt As Integer
    Protected _tcsl As Boolean

#Region "Property"
    Public Sub New()

    End Sub

    Public Sub New(ByVal obj As CPhieuNX)
        With obj
            _dpnx_id = .DPNX_ID
            _nx = .NX
            _loaimh = .LOAIMH
            _sophieu = .SOPHIEU
            _ngayph = .NGAYPH
            _dvgn = .DVGN
            _tcnx = .TCNX
            _lk = .LK
            _theolenh = .THEOLENH
            _hopdong = .HOPDONG
            _nguoign = .NGUOIGN
            _ggt = .GGT
            _ngaykggt = .NGAYKGGT
            _ngaygn = .NGAYGN
            _nguoivp = .NGUOIVP
            _ngay = .NGAY
            _sokh = .SOKH
            _sokien = .SOKIEN
            _trluong = .TRLUONG
            _httt = .HTTT
            _tgiatri = .TGIATRI
            _bcth = .BCTH
            _kyqt = .KYQT
            '_tcsl = .TCSL
        End With
    End Sub

    Public Property DPNX_ID() As Integer
        Get
            Return _dpnx_id
        End Get
        Set(ByVal value As Integer)
            _dpnx_id = value
        End Set
    End Property

    Public Property NX() As String
        Get
            Return _nx
        End Get
        Set(ByVal value As String)
            _nx = value
        End Set
    End Property

    Public Property LOAIMH() As String
        Get
            Return _loaimh
        End Get
        Set(ByVal value As String)
            _loaimh = value
        End Set
    End Property

    Public Property SOPHIEU() As String
        Get
            Return _sophieu
        End Get
        Set(ByVal value As String)
            _sophieu = value
        End Set
    End Property

    Public Property NGAYPH() As Date
        Get
            Return _ngayph
        End Get
        Set(ByVal value As Date)
            _ngayph = value
        End Set
    End Property

    Public Property DVGN() As String
        Get
            Return _dvgn
        End Get
        Set(ByVal value As String)
            _dvgn = value
        End Set
    End Property

    Public Property TCNX() As String
        Get
            Return _tcnx
        End Get
        Set(ByVal value As String)
            _tcnx = value
        End Set
    End Property

    Public Property LK() As String
        Get
            Return _lk
        End Get
        Set(ByVal value As String)
            _lk = value
        End Set
    End Property

    Public Property THEOLENH() As String
        Get
            Return _theolenh
        End Get
        Set(ByVal value As String)
            _theolenh = value
        End Set
    End Property

    Public Property HOPDONG() As Integer
        Get
            Return _hopdong
        End Get
        Set(ByVal value As Integer)
            _hopdong = value
        End Set
    End Property

    Public Property NGUOIGN() As String
        Get
            Return _nguoign
        End Get
        Set(ByVal value As String)
            _nguoign = value
        End Set
    End Property

    Public Property GGT() As String
        Get
            Return _ggt
        End Get
        Set(ByVal value As String)
            _ggt = value
        End Set
    End Property

    Public Property NGAYKGGT() As Date
        Get
            Return _ngaykggt
        End Get
        Set(ByVal value As Date)
            _ngaykggt = value
        End Set
    End Property

    Public Property NGAYGN() As Date
        Get
            Return _ngaygn
        End Get
        Set(ByVal value As Date)
            _ngaygn = value
        End Set
    End Property

    Public Property NGUOIVP() As String
        Get
            Return _nguoivp
        End Get
        Set(ByVal value As String)
            _nguoivp = value
        End Set
    End Property

    Public Property NGAY() As Date
        Get
            Return _ngay
        End Get
        Set(ByVal value As Date)
            _ngay = value
        End Set
    End Property

    Public Property SOKH() As Integer
        Get
            Return _sokh
        End Get
        Set(ByVal value As Integer)
            _sokh = value
        End Set
    End Property

    Public Property SOKIEN() As Integer
        Get
            Return _sokien
        End Get
        Set(ByVal value As Integer)
            _sokien = value
        End Set
    End Property

    Public Property TRLUONG() As Single
        Get
            Return _trluong
        End Get
        Set(ByVal value As Single)
            _trluong = value
        End Set
    End Property

    Public Property HTTT() As Integer
        Get
            Return _httt
        End Get
        Set(ByVal value As Integer)
            _httt = value
        End Set
    End Property

    Public Property TGIATRI() As Double
        Get
            Return _tgiatri
        End Get
        Set(ByVal value As Double)
            _tgiatri = value
        End Set
    End Property

    Public Property BCTH() As Integer
        Get
            Return _bcth
        End Get
        Set(ByVal value As Integer)
            _bcth = value
        End Set
    End Property

    Public Property KYQT() As Integer
        Get
            Return _kyqt
        End Get
        Set(ByVal value As Integer)
            _kyqt = value
        End Set
    End Property

    Public Property TCSL() As Boolean
        Get
            Return _tcsl
        End Get
        Set(ByVal value As Boolean)
            _tcsl = value
        End Set
    End Property
#End Region

    Public Overrides Function ToString() As String
        Dim str As String
        str = _sophieu
        While str.Length < 9
            str += " "
        End While

        Return str & " - " & Format(_ngay, FormatDateStyle()) & " - " & _tgiatri.ToString()
    End Function

    Public Function GetValue(ByVal value As Object) As Object
        If value Is Nothing Then
            Return DBNull.Value
        End If
        Return value
    End Function

    Public Function ColumnLength(ByVal column As String) As Integer
        Return CInt(BaseDB.ExecSql_DataValue("select COL_LENGTH('tblDPNX','" + column + "')")) / 2
    End Function

    ''' <summary>
    ''' 2014-03-18:
    ''' Kế hoạch xác nhận phiếu
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function KeHoach_XacNhan() As Long
        Dim lRowsAffected As Long = 0
        Try
            Dim par(0) As SqlClient.SqlParameter
            par(0) = New SqlParameter("@dpnx_id", SqlDbType.Decimal)
            par(0).Value = _dpnx_id
            lRowsAffected = BaseDB.ExecSqlProcedure_NoneQuery("sp_KeHoach_XacNhan", par)
            BCTH = enumBCTH.KH
        Catch ex As Exception
            Throw ex
        End Try
        Return lRowsAffected
    End Function

    '''' <summary>
    '''' 2014-03-18:
    '''' Kế hoạch hủy xác nhận phiếu
    '''' </summary>
    '''' <returns></returns>
    '''' <remarks></remarks>
    'Public Function KeHoach_HuyXacNhan() As Long
    '    Dim lRowsAffected As Long = 0
    '    Try
    '        Dim par(0) As SqlClient.SqlParameter
    '        par(0) = New SqlParameter("@dpnx_id", SqlDbType.Decimal)
    '        par(0).Value = _dpnx_id
    '        lRowsAffected = BaseDB.ExecSqlProcedure_NoneQuery("sp_KeHoach_HuyXacNhan", par)
    '    Catch ex As Exception
    '        Throw ex
    '    End Try
    '    Return lRowsAffected
    'End Function

    ''' <summary>
    ''' 2014-03-18:
    ''' Kho xác nhận phiếu
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Kho_XacNhan() As Long
        Dim lRowsAffected As Long = 0
        Dim par(0) As SqlClient.SqlParameter
        Try
            par(0) = New SqlParameter("@dpnx_id", SqlDbType.Decimal)
            par(0).Value = _dpnx_id
            lRowsAffected = BaseDB.ExecSqlProcedure_NoneQuery("sp_Kho_XacNhan", par)
            BCTH = enumBCTH.KHO
        Catch ex As Exception
            Throw ex
        End Try
        Return lRowsAffected
    End Function

    ''' <summary>
    ''' 2014-03-18:
    ''' Kho hủy xác nhận phiếu
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Kho_HuyXacNhan() As Long
        Dim lRowsAffected As Long = 0
        Try
            Dim par(0) As SqlClient.SqlParameter
            par(0) = New SqlParameter("@dpnx_id", SqlDbType.Decimal)
            par(0).Value = _dpnx_id
            lRowsAffected = BaseDB.ExecSqlProcedure_NoneQuery("sp_Kho_HuyXacNhan", par)
            BCTH = enumBCTH.KH
        Catch ex As Exception
            Throw ex
        End Try
        Return lRowsAffected
    End Function

    ''' <summary>
    ''' 2014-03-18:
    ''' Tài chính SL xác nhận phiếu
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function TCSL_XacNhan() As Long
        Dim lRowsAffected As Long = 0
        Try
            Dim par(0) As SqlClient.SqlParameter
            par(0) = New SqlParameter("@dpnx_id", SqlDbType.Decimal)
            par(0).Value = _dpnx_id
            lRowsAffected = BaseDB.ExecSqlProcedure_NoneQuery("sp_TCSL_XacNhan", par)
            BCTH = enumBCTH.TCSL
        Catch ex As Exception
            Throw ex
        End Try
        Return lRowsAffected
    End Function

    ''' <summary>
    ''' 2014-03-18:
    ''' Tài chính SL hủy xác nhận phiếu
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function TCSL_HuyXacNhan() As Long
        Dim lRowsAffected As Long = 0
        Try
            Dim par(0) As SqlClient.SqlParameter
            par(0) = New SqlParameter("@dpnx_id", SqlDbType.Decimal)
            par(0).Value = _dpnx_id
            lRowsAffected = BaseDB.ExecSqlProcedure_NoneQuery("sp_TCSL_HuyXacNhan", par)
            BCTH = enumBCTH.KHO
        Catch ex As Exception
            Throw ex
        End Try
        Return lRowsAffected
    End Function

    ''' <summary>
    ''' Tài chính xác nhận tiền
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function TCT_XacNhan() As Long
        Dim lRowsAffected As Long = 0
        Try
            Dim par(1) As SqlClient.SqlParameter
            par(0) = New SqlParameter("@dpnx_id", SqlDbType.Decimal)
            par(0).Value = _dpnx_id
            par(1) = New SqlParameter("@kyqt", SqlDbType.Decimal)
            par(1).Value = _kyqt
            lRowsAffected = BaseDB.ExecSqlProcedure_NoneQuery("sp_TCT_XacNhan", par)
            BCTH = enumBCTH.TC
        Catch ex As Exception
            Throw ex
        End Try
        Return lRowsAffected
    End Function

    ''' <summary>
    ''' Tài chính tiền hủy xác nhận phiếu
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function TCT_HuyXacNhan() As Long
        Dim lRowsAffected As Long = 0
        Try
            Dim par(0) As SqlClient.SqlParameter
            par(0) = New SqlParameter("@dpnx_id", SqlDbType.Decimal)
            par(0).Value = _dpnx_id
            lRowsAffected = BaseDB.ExecSqlProcedure_NoneQuery("sp_TCT_HuyXacNhan", par)
            BCTH = enumBCTH.TCSL
        Catch ex As Exception
            Throw ex
        End Try
        Return lRowsAffected
    End Function

    '''' <summary>
    '''' Tài chính xác nhận số lượng
    '''' </summary>
    '''' <returns></returns>
    '''' <remarks></remarks>
    'Public Function TaiChinhXacNhanSL() As Long
    '    Dim lRowsAffected As Long = 0
    '    Try
    '        Dim par(1) As SqlClient.SqlParameter
    '        par(0) = New SqlParameter("@dpnx_id", SqlDbType.Decimal)
    '        par(0).Value = _dpnx_id
    '        par(1) = New SqlParameter("@tcsl", SqlDbType.Bit)
    '        par(1).Value = 1
    '        lRowsAffected = BaseDB.ExecSqlProcedure_NoneQuery("sp_CapNhatTaiChinhSoLuong", par)
    '    Catch ex As Exception
    '        Throw ex
    '    End Try
    '    Return lRowsAffected
    'End Function

    '''' <summary>
    '''' Tài chính xác nhận số lượng
    '''' </summary>
    '''' <returns></returns>
    '''' <remarks></remarks>
    'Public Function TaiChinhHuyXacNhanSL() As Long
    '    Dim lRowsAffected As Long = 0
    '    Try
    '        Dim par(1) As SqlClient.SqlParameter
    '        par(0) = New SqlParameter("@dpnx_id", SqlDbType.Decimal)
    '        par(0).Value = _dpnx_id
    '        par(1) = New SqlParameter("@tcsl", SqlDbType.Bit)
    '        par(1).Value = 0
    '        lRowsAffected = BaseDB.ExecSqlProcedure_NoneQuery("sp_CapNhatTaiChinhSoLuong", par)
    '    Catch ex As Exception
    '        Throw ex
    '    End Try
    '    Return lRowsAffected
    'End Function

    Public Function Insert() As Long
        Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
        Dim objDBUtility As IDBUtility = objConnection.DBUtility

        Dim objStringBuilder As New StringBuilder(250)
        With objStringBuilder
            .Append("INSERT INTO tblDPNX(" & _
                    "nx, loaimh, sophieu, ngayph, dvgn, tcnx, lk, theolenh, hopdong" & _
                    ", nguoign, ggt, ngaykggt, ngaygn, nguoivp, ngay" & _
                    ", sokh, sokien, trluong, httt, tgiatri, bcth, kyqt) VALUES(")
            .Append(objDBUtility.GetParamPlaceHolder("nx", True))
            .Append(objDBUtility.GetParamPlaceHolder("loaimh", True))
            .Append(objDBUtility.GetParamPlaceHolder("sophieu", True))
            .Append(objDBUtility.GetParamPlaceHolder("ngayph", True))
            .Append(objDBUtility.GetParamPlaceHolder("dvgn", True))
            .Append(objDBUtility.GetParamPlaceHolder("tcnx", True))
            .Append(objDBUtility.GetParamPlaceHolder("lk", True))
            .Append(objDBUtility.GetParamPlaceHolder("theolenh", True))
            .Append(objDBUtility.GetParamPlaceHolder("hopdong", True))
            .Append(objDBUtility.GetParamPlaceHolder("nguoign", True))
            .Append(objDBUtility.GetParamPlaceHolder("ggt", True))
            .Append(objDBUtility.GetParamPlaceHolder("ngaykggt", True))
            .Append(objDBUtility.GetParamPlaceHolder("ngaygn", True))
            .Append(objDBUtility.GetParamPlaceHolder("nguoivp", True))
            .Append(objDBUtility.GetParamPlaceHolder("ngay", True))
            .Append(objDBUtility.GetParamPlaceHolder("sokh", True))
            .Append(objDBUtility.GetParamPlaceHolder("sokien", True))
            .Append(objDBUtility.GetParamPlaceHolder("trluong", True))
            .Append(objDBUtility.GetParamPlaceHolder("httt", True))
            .Append(objDBUtility.GetParamPlaceHolder("tgiatri", True))
            .Append(objDBUtility.GetParamPlaceHolder("bcth", True))
            .Append(objDBUtility.GetParamPlaceHolder("kyqt", False))
            .Append(")")
        End With

        Dim sInsert As String = objStringBuilder.ToString

        Dim objCommand As IDbCommand = objConnection.CreateCommand(sInsert)
        Dim objTransaction As IDbTransaction = objConnection.BeginTransaction

        Try
            With objCommand
                .Transaction = objTransaction
                .Parameters.Add(objDBUtility.CreateParameter("nx", DbType.String, _nx, _nx.Length))
                .Parameters.Add(objDBUtility.CreateParameter("loaimh", DbType.String, _loaimh, _loaimh.Length))
                .Parameters.Add(objDBUtility.CreateParameter("sophieu", DbType.String, _sophieu, _sophieu.Length))
                .Parameters.Add(objDBUtility.CreateParameter("ngayph", DbType.Date, GetDateParam(_ngayph), 0))
                Try
                    .Parameters.Add(objDBUtility.CreateParameter("dvgn", DbType.String, _dvgn, _dvgn.Length))
                Catch ex As Exception
                    .Parameters.Add(objDBUtility.CreateParameter("dvgn", DbType.String, DBNull.Value, 0))
                End Try

                Try
                    .Parameters.Add(objDBUtility.CreateParameter("tcnx", DbType.String, _tcnx, _tcnx.Length))
                Catch ex As Exception
                    .Parameters.Add(objDBUtility.CreateParameter("tcnx", DbType.String, DBNull.Value, 0))
                End Try

                .Parameters.Add(objDBUtility.CreateParameter("lk", DbType.String, _lk, _lk.Length))
                .Parameters.Add(objDBUtility.CreateParameter("theolenh", DbType.String, _theolenh, _theolenh.Length))

                If _hopdong > 0 Then
                    .Parameters.Add(objDBUtility.CreateParameter("hopdong", DbType.Decimal, _hopdong, 0))
                Else
                    .Parameters.Add(objDBUtility.CreateParameter("hopdong", DbType.Decimal, DBNull.Value, 0))
                End If

                Try
                    .Parameters.Add(objDBUtility.CreateParameter("nguoign", DbType.String, _nguoign, _nguoign.Length))
                Catch ex As Exception
                    .Parameters.Add(objDBUtility.CreateParameter("nguoign", DbType.String, DBNull.Value, 0))
                End Try

                If _nx = "N" Then
                    .Parameters.Add(objDBUtility.CreateParameter("ggt", DbType.String, DBNull.Value, 0))
                    .Parameters.Add(objDBUtility.CreateParameter("ngaykggt", DbType.Date, DBNull.Value, 0))
                Else
                    Try
                        .Parameters.Add(objDBUtility.CreateParameter("ggt", DbType.String, _ggt, GGT.Length))
                    Catch ex As Exception
                        .Parameters.Add(objDBUtility.CreateParameter("ggt", DbType.String, DBNull.Value, 0))
                    End Try
                    .Parameters.Add(objDBUtility.CreateParameter("ngaykggt", DbType.Date, GetDateParam(_ngaykggt), 0))
                End If
                .Parameters.Add(objDBUtility.CreateParameter("ngaygn", DbType.Date, GetDateParam(_ngaygn), 0))
                .Parameters.Add(objDBUtility.CreateParameter("nguoivp", DbType.String, _nguoivp, _nguoivp.Length))
                .Parameters.Add(objDBUtility.CreateParameter("ngay", DbType.Date, _ngay, 0))
                .Parameters.Add(objDBUtility.CreateParameter("sokh", DbType.Decimal, _sokh, 0))
                .Parameters.Add(objDBUtility.CreateParameter("sokien", DbType.Decimal, _sokien, 0))
                .Parameters.Add(objDBUtility.CreateParameter("trluong", DbType.Decimal, _trluong, 0))
                .Parameters.Add(objDBUtility.CreateParameter("httt", DbType.Decimal, _httt, 0))
                .Parameters.Add(objDBUtility.CreateParameter("tgiatri", DbType.Decimal, _tgiatri, 0))
                .Parameters.Add(objDBUtility.CreateParameter("bcth", DbType.Decimal, _bcth, 0))
                .Parameters.Add(objDBUtility.CreateParameter("kyqt", DbType.Decimal, _kyqt, 0))
            End With

            _dpnx_id = objDBUtility.InsertAndReturnSerialKey(objCommand, "dpnx_id", "tblDPNX")
            objTransaction.Commit()
        Catch ex As System.Exception
            objTransaction.Rollback()
            Throw ex
        Finally
            objConnection.Close()
        End Try
        Return _dpnx_id
    End Function

    Public Function Update() As Long
        Dim lRowsAffected As Long = 0
        Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
        Dim objDBUtility As IDBUtility = objConnection.DBUtility

        Dim strSQL As String = "UPDATE tblDPNX SET"
        strSQL &= " NX = " & objDBUtility.GetParamPlaceHolder("NX", True)
        strSQL &= " LOAIMH = " & objDBUtility.GetParamPlaceHolder("LOAIMH", True)
        strSQL &= " SOPHIEU = " & objDBUtility.GetParamPlaceHolder("SOPHIEU", True)
        strSQL &= " NgAYPH = " & objDBUtility.GetParamPlaceHolder("NgayPH", True)
        strSQL &= " DVGN = " & objDBUtility.GetParamPlaceHolder("DVGN", True)
        strSQL &= " TCNX = " & objDBUtility.GetParamPlaceHolder("TCNX", True)
        strSQL &= " LK = " & objDBUtility.GetParamPlaceHolder("LK", True)
        strSQL &= " THEOLENH = " & objDBUtility.GetParamPlaceHolder("THEOLENH", True)
        strSQL &= " HOPDONG = " & objDBUtility.GetParamPlaceHolder("HOPDONG", True)
        strSQL &= " NGUOIGN = " & objDBUtility.GetParamPlaceHolder("NGUOIGN", True)
        strSQL &= " GGT = " & objDBUtility.GetParamPlaceHolder("GGT", True)
        strSQL &= " NgayKGGT = " & objDBUtility.GetParamPlaceHolder("NgayKGGT", True)
        strSQL &= " NGAYGN = " & objDBUtility.GetParamPlaceHolder("NGAYGN", True)
        strSQL &= " NGUOIVP = " & objDBUtility.GetParamPlaceHolder("NGUOIVP", True)
        strSQL &= " NGAY = " & objDBUtility.GetParamPlaceHolder("NGAY", True)
        strSQL &= " SOKH = " & objDBUtility.GetParamPlaceHolder("SOKH", True)
        strSQL &= " SOKIEN = " & objDBUtility.GetParamPlaceHolder("SOKIEN", True)
        strSQL &= " TRLUONG = " & objDBUtility.GetParamPlaceHolder("TRLUONG", True)
        strSQL &= " HTTT = " & objDBUtility.GetParamPlaceHolder("HTTT", True)
        strSQL &= " TGIATRI = " & objDBUtility.GetParamPlaceHolder("TGIATRI", True)
        strSQL &= " BCTH = " & objDBUtility.GetParamPlaceHolder("BCTH", True)
        strSQL &= " KYQT = " & objDBUtility.GetParamPlaceHolder("KYQT", False)
        strSQL &= " WHERE DPNX_ID = " & objDBUtility.GetParamPlaceHolder("DPNX_ID", False)

        Dim objTransaction As IDbTransaction = objConnection.BeginTransaction
        Dim objCommand As IDbCommand = objConnection.CreateCommand(strSQL)

        Try
            With objCommand
                .Transaction = objTransaction
                .Parameters.Add(objDBUtility.CreateParameter("nx", DbType.String, _nx, _nx.Length))
                .Parameters.Add(objDBUtility.CreateParameter("loaimh", DbType.String, _loaimh, _loaimh.Length))
                .Parameters.Add(objDBUtility.CreateParameter("sophieu", DbType.String, _sophieu, _sophieu.Length))
                .Parameters.Add(objDBUtility.CreateParameter("ngayph", DbType.Date, GetDateParam(_ngayph), 0))
                .Parameters.Add(objDBUtility.CreateParameter("dvgn", DbType.String, _dvgn, _dvgn.Length))
                Try
                    .Parameters.Add(objDBUtility.CreateParameter("tcnx", DbType.String, _tcnx, _tcnx.Length))
                Catch ex As Exception
                    .Parameters.Add(objDBUtility.CreateParameter("tcnx", DbType.String, DBNull.Value, 0))
                End Try
                .Parameters.Add(objDBUtility.CreateParameter("lk", DbType.String, _lk, _lk.Length))
                .Parameters.Add(objDBUtility.CreateParameter("theolenh", DbType.String, _theolenh, _theolenh.Length))
                Try
                    .Parameters.Add(objDBUtility.CreateParameter("hopdong", DbType.Decimal, _hopdong, 0))
                Catch ex As Exception
                    .Parameters.Add(objDBUtility.CreateParameter("hopdong", DbType.Decimal, DBNull.Value, 0))
                End Try

                Try
                    .Parameters.Add(objDBUtility.CreateParameter("nguoign", DbType.String, _nguoign, _nguoign.Length))
                Catch ex As Exception
                    .Parameters.Add(objDBUtility.CreateParameter("nguoign", DbType.String, DBNull.Value, 0))
                End Try

                If _nx = "N" Then
                    .Parameters.Add(objDBUtility.CreateParameter("ggt", DbType.String, DBNull.Value, 0))
                    .Parameters.Add(objDBUtility.CreateParameter("ngaykggt", DbType.Date, DBNull.Value, 0))
                Else
                    Try
                        .Parameters.Add(objDBUtility.CreateParameter("ggt", DbType.String, _ggt, GGT.Length))
                    Catch ex As Exception
                        .Parameters.Add(objDBUtility.CreateParameter("ggt", DbType.String, DBNull.Value, 0))
                    End Try
                    .Parameters.Add(objDBUtility.CreateParameter("ngaykggt", DbType.Date, GetDateParam(_ngaykggt), 0))
                End If
                .Parameters.Add(objDBUtility.CreateParameter("ngaygn", DbType.Date, GetDateParam(_ngaygn), 0))
                .Parameters.Add(objDBUtility.CreateParameter("nguoivp", DbType.String, _nguoivp, _nguoivp.Length))
                .Parameters.Add(objDBUtility.CreateParameter("ngay", DbType.Date, _ngay, 0))
                .Parameters.Add(objDBUtility.CreateParameter("sokh", DbType.Decimal, _sokh, 0))
                .Parameters.Add(objDBUtility.CreateParameter("sokien", DbType.Decimal, _sokien, 0))
                .Parameters.Add(objDBUtility.CreateParameter("trluong", DbType.Decimal, _trluong, 0))
                .Parameters.Add(objDBUtility.CreateParameter("httt", DbType.Decimal, _httt, 0))
                .Parameters.Add(objDBUtility.CreateParameter("tgiatri", DbType.Decimal, _tgiatri, 0))
                .Parameters.Add(objDBUtility.CreateParameter("bcth", DbType.Decimal, _bcth, 0))
                .Parameters.Add(objDBUtility.CreateParameter("kyqt", DbType.Decimal, _kyqt, 0))
                .Parameters.Add(objDBUtility.CreateParameter("dpnx_id", DbType.Decimal, _dpnx_id, 0))
                lRowsAffected = .ExecuteNonQuery()
                objTransaction.Commit()

                
            End With

        Catch ex As System.Exception
            objTransaction.Rollback()
            Throw ex
        Finally
            objConnection.Close()
        End Try

        Return lRowsAffected
    End Function

    Public Function Delete() As Long
        Dim lRowsAffected As Long = 0
        Try
            Dim par(0) As SqlClient.SqlParameter
            par(0) = New SqlParameter("@dpnx_id", SqlDbType.Decimal)
            par(0).Value = _dpnx_id
            lRowsAffected = BaseDB.ExecSqlProcedure_NoneQuery("sp_XoaPhieuNX", par)

            '==========================
            'Write log
            CLogs.WriteLog_PhieuNX(enumAction.Xoa, Me)
        Catch ex As Exception
            Throw ex
        End Try
        Return lRowsAffected
    End Function
End Class

Public Class CPhieuNXs
    ''' <summary>
    ''' Update: 25-1-2010
    ''' Kiểm tra xem số phiếu nhập có bị trùng không
    ''' </summary>
    ''' <param name="loai"></param>
    ''' <param name="pSoPhieu"></param>
    ''' <param name="id"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function IsExist(ByVal loai As enumDOITUONG, ByVal pSoPhieu As String, Optional ByVal id As Integer = -1) As Boolean
        Dim strSQL As String = "select * from tblDPNX where sophieu = N'" + pSoPhieu + "' and NX = '" + LocNhapXuat(loai) + "'"
        If id > 0 Then
            strSQL += " and dpnx_id <> " + id.ToString()
        End If
        Dim objReader As SqlDataReader
        objReader = BaseDB.ExecSql_DataReader(strSQL)
        If objReader.Read() Then
            objReader.Close()
            Return True
        End If
        Return False
    End Function

    'Kiểm tra xem có còn phiếu nx nào chưa được tài chính xác nhận không?
    Public Shared Function IsTatCaPhieuDaXacNhanTC() As Boolean
        Dim strSQL As String = "select * from tblDPNX where BCTH = 1"
        Dim objReader As SqlDataReader
        objReader = BaseDB.ExecSql_DataReader(strSQL)
        If objReader.Read() Then
            objReader.Close()
            Return False
        End If
        Return True
    End Function

    Public Shared Function GetDPNX_IDBySoPhieu(ByVal loai As enumDOITUONG, ByVal sSoPhieu As String) As Integer
        Dim strSQL As String = "select dpnx_id from tblDPNX where sophieu = N'" + sSoPhieu + "' and NX = '" + LocNhapXuat(loai) + "'"
        Dim dpnx_id As Integer = -1
        Try
            dpnx_id = CInt(BaseDB.ExecSql_DataValue(strSQL))
        Catch ex As System.Exception
            Throw ex
        End Try
        Return dpnx_id
    End Function

    Public Shared Function GetSoPhieuByDPNX_ID(ByVal mDPNX_ID As Integer) As String
        Dim strSQL As String = "select sophieu from tblDPNX where dpnx_id = " + mDPNX_ID.ToString()
        Dim sophieu As String = ""
        Try
            sophieu = CStr(BaseDB.ExecSql_DataValue(strSQL))
        Catch ex As System.Exception
            Throw ex
        End Try
        Return sophieu
    End Function

    ''' <summary>
    ''' Đọc object CPhieuNX từ SqlDataReader
    ''' </summary>
    ''' <param name="objReader"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetPhieuNX(ByVal objReader As SqlDataReader) As CPhieuNX
        Dim objPhieuNX As CPhieuNX = New CPhieuNX
        With objPhieuNX
            .DPNX_ID = BaseDB.GetObjectValue(objReader.GetValue(0))
            .NX = BaseDB.GetObjectValue(objReader.GetValue(1))
            .LOAIMH = BaseDB.GetObjectValue(objReader.GetValue(2))
            .SOPHIEU = BaseDB.GetObjectValue(objReader.GetValue(3))

            Try
                .NGAYPH = objReader.GetDateTime(4)
            Catch ex As Exception
                .NGAYPH = DateNull()
            End Try

            .DVGN = BaseDB.GetObjectValue(objReader.GetValue(5))
            .TCNX = BaseDB.GetObjectValue(objReader.GetValue(6))
            .LK = BaseDB.GetObjectValue(objReader.GetValue(7))
            .THEOLENH = BaseDB.GetObjectValue(objReader.GetValue(8))
            .HOPDONG = BaseDB.GetObjectValue(objReader.GetValue(9))
            .NGUOIGN = BaseDB.GetObjectValue(objReader.GetValue(10))
            .GGT = BaseDB.GetObjectValue(objReader.GetValue(11))
            Try
                .NGAYKGGT = objReader.GetDateTime(12)
            Catch ex As Exception
                .NGAYKGGT = DateNull()
            End Try

            Try
                .NGAYGN = objReader.GetDateTime(13)
            Catch ex As Exception
                .NGAYGN = DateNull()
            End Try

            .NGUOIVP = BaseDB.GetObjectValue(objReader.GetValue(14))

            Try
                .NGAY = objReader.GetDateTime(15)
            Catch ex As Exception
                .NGAY = DateNull()
            End Try

            .SOKH = BaseDB.GetObjectValue(objReader.GetValue(16))
            .SOKIEN = BaseDB.GetObjectValue(objReader.GetValue(17))
            .TRLUONG = BaseDB.GetObjectValue(objReader.GetValue(18))
            .HTTT = BaseDB.GetObjectValue(objReader.GetValue(19))
            .TGIATRI = BaseDB.GetObjectValue(objReader.GetValue(20))
            .BCTH = BaseDB.GetObjectValue(objReader.GetValue(21))
            .KYQT = BaseDB.GetObjectValue(objReader.GetValue(22))
            '.TCSL = objReader.GetBoolean(23)
        End With
        Return objPhieuNX
    End Function

    Public Shared Function GetPhieuBySoPhieu(ByVal loai As enumDOITUONG, ByVal pSoPhieu As String) As CPhieuNX
        Dim strSQL As String = "select * from tblDPNX where sophieu = N'" + pSoPhieu + "' and NX = '" + LocNhapXuat(loai) + "'"
        Dim objReader As SqlDataReader = Nothing
        Dim objPhieuNX As CPhieuNX = Nothing
        Try
            objReader = BaseDB.ExecSql_DataReader(strSQL)
            If objReader.Read() Then
                objPhieuNX = GetPhieuNX(objReader)
                'cls = New CPhieuNX
                'With cls
                '    .DPNX_ID = BaseDB.GetObjectValue(objReader.GetValue(0))
                '    .NX = BaseDB.GetObjectValue(objReader.GetValue(1))
                '    .LOAIMH = BaseDB.GetObjectValue(objReader.GetValue(2))
                '    .SOPHIEU = BaseDB.GetObjectValue(objReader.GetValue(3))

                '    Try
                '        .NGAYPH = objReader.GetDateTime(4)
                '    Catch ex As Exception
                '        .NGAYPH = DateNull()
                '    End Try

                '    .DVGN = BaseDB.GetObjectValue(objReader.GetValue(5))
                '    .TCNX = BaseDB.GetObjectValue(objReader.GetValue(6))
                '    .LK = BaseDB.GetObjectValue(objReader.GetValue(7))
                '    .THEOLENH = BaseDB.GetObjectValue(objReader.GetValue(8))
                '    .HOPDONG = BaseDB.GetObjectValue(objReader.GetValue(9))
                '    .NGUOIGN = BaseDB.GetObjectValue(objReader.GetValue(10))
                '    .GGT = BaseDB.GetObjectValue(objReader.GetValue(11))
                '    Try
                '        .NGAYKGGT = objReader.GetDateTime(12)
                '    Catch ex As Exception
                '        .NGAYKGGT = DateNull()
                '    End Try

                '    Try
                '        .NGAYGN = objReader.GetDateTime(13)
                '    Catch ex As Exception
                '        .NGAYGN = DateNull()
                '    End Try

                '    .NGUOIVP = BaseDB.GetObjectValue(objReader.GetValue(14))

                '    Try
                '        .NGAY = objReader.GetDateTime(15)
                '    Catch ex As Exception
                '        .NGAY = DateNull()
                '    End Try

                '    .SOKH = BaseDB.GetObjectValue(objReader.GetValue(16))
                '    .SOKIEN = BaseDB.GetObjectValue(objReader.GetValue(17))
                '    .TRLUONG = BaseDB.GetObjectValue(objReader.GetValue(18))
                '    .HTTT = BaseDB.GetObjectValue(objReader.GetValue(19))
                '    .TGIATRI = BaseDB.GetObjectValue(objReader.GetValue(20))
                '    .BCTH = BaseDB.GetObjectValue(objReader.GetValue(21))
                '    .KYQT = BaseDB.GetObjectValue(objReader.GetValue(22))
                '    .TCSL = objReader.GetBoolean(23)
                'End With
            End If
        Catch ex As System.Exception
            Throw ex
        Finally
            If Not IsNothing(objReader) Then
                objReader.Close()
            End If
        End Try
        Return objPhieuNX
    End Function

    Public Shared Function GetPhieuByMa(ByVal mDPNX_ID As Integer) As CPhieuNX
        Dim strSQL As String = "select * from tblDPNX where dpnx_id = " + mDPNX_ID.ToString()
        Dim objReader As SqlDataReader = Nothing
        Dim objPhieuNX As CPhieuNX = Nothing
        Try
            objReader = BaseDB.ExecSql_DataReader(strSQL)
            If objReader.Read() Then
                objPhieuNX = GetPhieuNX(objReader)
            End If

            'objReader.Read()
            'With cls
            '    .DPNX_ID = BaseDB.GetObjectValue(objReader.GetValue(0))
            '    .NX = BaseDB.GetObjectValue(objReader.GetValue(1))
            '    .LOAIMH = BaseDB.GetObjectValue(objReader.GetValue(2))
            '    .SOPHIEU = BaseDB.GetObjectValue(objReader.GetValue(3))

            '    Try
            '        .NGAYPH = objReader.GetDateTime(4)
            '    Catch ex As Exception
            '        .NGAYPH = DateNull()
            '    End Try

            '    .DVGN = BaseDB.GetObjectValue(objReader.GetValue(5))
            '    .TCNX = BaseDB.GetObjectValue(objReader.GetValue(6))
            '    .LK = BaseDB.GetObjectValue(objReader.GetValue(7))
            '    .THEOLENH = BaseDB.GetObjectValue(objReader.GetValue(8))
            '    .HOPDONG = BaseDB.GetObjectValue(objReader.GetValue(9))
            '    .NGUOIGN = BaseDB.GetObjectValue(objReader.GetValue(10))
            '    .GGT = BaseDB.GetObjectValue(objReader.GetValue(11))
            '    Try
            '        .NGAYKGGT = objReader.GetDateTime(12)
            '    Catch ex As Exception
            '        .NGAYKGGT = DateNull()
            '    End Try

            '    Try
            '        .NGAYGN = objReader.GetDateTime(13)
            '    Catch ex As Exception
            '        .NGAYGN = DateNull()
            '    End Try

            '    .NGUOIVP = BaseDB.GetObjectValue(objReader.GetValue(14))

            '    Try
            '        .NGAY = objReader.GetDateTime(15)
            '    Catch ex As Exception
            '        .NGAY = DateNull()
            '    End Try

            '    .SOKH = BaseDB.GetObjectValue(objReader.GetValue(16))
            '    .SOKIEN = BaseDB.GetObjectValue(objReader.GetValue(17))
            '    .TRLUONG = BaseDB.GetObjectValue(objReader.GetValue(18))
            '    .HTTT = BaseDB.GetObjectValue(objReader.GetValue(19))
            '    .TGIATRI = BaseDB.GetObjectValue(objReader.GetValue(20))
            '    .BCTH = BaseDB.GetObjectValue(objReader.GetValue(21))
            '    .KYQT = BaseDB.GetObjectValue(objReader.GetValue(22))
            '    .TCSL = objReader.GetBoolean(23)
            'End With
        Catch ex As System.Exception
            Throw ex
        Finally
            If Not IsNothing(objReader) Then
                objReader.Close()
            End If
        End Try
        Return objPhieuNX
    End Function

    'Public Shared Function GetBCTHofPhieu(ByVal loai As enumDOITUONG, ByVal pSoPhieu As String) As Integer
    '    Dim strSQL As String = "select BCTH from tblDPNX where sophieu = N'" + pSoPhieu + "' and NX = '" + LocNhapXuat(loai) + "'"
    '    Dim objReader As SqlDataReader = Nothing
    '    Dim BCTH As Integer
    '    Try
    '        objReader = BaseDB.ExecSql_DataReader(strSQL)
    '        If objReader.Read() Then
    '            BCTH = BaseDB.GetObjectValue(objReader.GetValue(20))
    '        End If
    '    Catch ex As System.Exception
    '        Throw ex
    '    Finally
    '        If Not IsNothing(objReader) Then
    '            objReader.Close()
    '        End If
    '    End Try
    '    Return BCTH
    'End Function

    Public Shared Function GetPhieuNXList(ByVal strDK As String) As ArrayList
        Dim aryItems As New ArrayList
        Dim strSQL As String
        Dim strWHERE As String = ""

        If strDK.Length > 0 Then
            strWHERE = " WHERE " & strDK & ""
        End If

        'strSQL = "SELECT dpnx_id, nx, sophieu, ngay, bcth, tgiatri,kyqt, tcsl FROM tblDPNX"
        strSQL = "SELECT dpnx_id, nx, sophieu, ngay, bcth, tgiatri, kyqt FROM tblDPNX"
        strSQL &= strWHERE
        strSQL &= " ORDER BY ngay desc, sophieu desc"
        Dim objReader As SqlDataReader = Nothing
        Try
            Dim objItem As CPhieuNX
            objReader = BaseDB.ExecSql_DataReader(strSQL)
            While objReader.Read
                'objItem = GetPhieuNX(objReader)
                objItem = New CPhieuNX
                With objItem
                    .DPNX_ID = objReader.GetDecimal(0)
                    .NX = objReader.GetString(1)
                    .SOPHIEU = objReader.GetString(2)
                    .NGAY = objReader.GetValue(3)
                    .BCTH = objReader.GetDecimal(4)
                    .TGIATRI = objReader.GetValue(5)
                    .KYQT = objReader.GetValue(6)
                    '.TCSL = objReader.GetBoolean(7)
                End With
                aryItems.Add(objItem)
            End While
        Catch ex As System.Exception
            Throw ex
        Finally
            If Not IsNothing(objReader) Then
                objReader.Close()
            End If
        End Try
        Return aryItems
    End Function

    Public Shared Function AddDataRow(ByRef myDataTable As DataTable, ByVal column1 As String, Optional ByVal column2 As String = "") As DataRow
        Dim myDataRow As DataRow
        myDataRow = myDataTable.NewRow()
        myDataRow("ThongSo") = column1
        myDataRow("GiaTri") = ""
        If column2 <> "" Then
            myDataRow("ThongSo1") = column2
            myDataRow("GiaTri1") = ""
        End If
        myDataTable.Rows.Add(myDataRow)
        Return myDataRow
    End Function

    Private Shared Sub PopulateHeaderRowPhieuNhap(ByRef myDataTable As DataTable)
        AddDataRow(myDataTable, "Số phiếu", "Số khoản")
        AddDataRow(myDataTable, "Đơn vị giao", "Số kiện")
        AddDataRow(myDataTable, "Tính chất nhập", "Trọng lượng(kg)")
        AddDataRow(myDataTable, "Loại kho", "HT thanh toán")
        AddDataRow(myDataTable, "Theo chỉ lệnh", "Tổng giá trị")
        AddDataRow(myDataTable, "Người giao", "Số Hợp đồng")
        AddDataRow(myDataTable, "Ngày giao", "Kỳ quyết toán")
        AddDataRow(myDataTable, "Người viết phiếu", "Hạn phiếu")
        AddDataRow(myDataTable, "Ngày viết phiếu")
    End Sub


    Private Shared Sub PopulateDataPhieuNhap(ByRef myDataTable As DataTable, ByVal dr As DataRowView)
        'Số phiếu
        myDataTable.Rows(0).Item("GiaTri") = dr.Item("SOPHIEU")
        'Số khoản
        myDataTable.Rows(0).Item("GiaTri1") = dr.Item("SOKH")

        'Đơn vị giao nhận
        myDataTable.Rows(1).Item("GiaTri") = CDoiTuongs.GetNameByMa(enumDOITUONG.DONVI, dr.Item("DVGN"))
        'Số kiện
        myDataTable.Rows(1).Item("GiaTri1") = Format(dr.Item("SOKIEN"), FormatQuantityStyle())

        'Tính chất nhập
        myDataTable.Rows(2).Item("GiaTri") = CDoiTuongs.GetNameByMa(enumDOITUONG.TINHCHATNHAP, dr.Item("TCNX"))
        'Trọng lượng
        myDataTable.Rows(2).Item("GiaTri1") = Format(dr.Item("TRLUONG"), FormatQuantityStyle())

        'Loại kho
        myDataTable.Rows(3).Item("GiaTri") = CDoiTuongs.GetNameByMa(enumDOITUONG.LOAIKHO, dr.Item("LK"))
        'HT thanh toán
        Try
            myDataTable.Rows(3).Item("GiaTri1") = CDoiTuongs.GetNameByMa(enumDOITUONG.THANHTOAN, dr.Item("HTTT"))
        Catch ex As Exception
            myDataTable.Rows(3).Item("GiaTri1") = ""
        End Try

        'Theo chỉ lệnh
        myDataTable.Rows(4).Item("GiaTri") = dr.Item("THEOLENH")
        'Tổng giá trị
        Try
            myDataTable.Rows(4).Item("GiaTri1") = CType(dr.Item("TGIATRI"), Double).ToString(FormatMoneyStyle())
        Catch ex As Exception
            myDataTable.Rows(4).Item("GiaTri1") = ""
        End Try

        'Người giao
        myDataTable.Rows(5).Item("GiaTri") = dr.Item("NGUOIGN")
        'Số Hợp đồng
        Try
            myDataTable.Rows(5).Item("GiaTri1") = CHopDongs.GetSoHDByHopDong_ID(dr.Item("HOPDONG"))
        Catch ex As Exception
            myDataTable.Rows(5).Item("GiaTri1") = ""
        End Try

        'Ngày giao
        Try
            myDataTable.Rows(6).Item("GiaTri") = Format(dr.Item("NGAYGN"), FormatDateStyle())
        Catch ex As Exception
            myDataTable.Rows(6).Item("GiaTri") = ""
        End Try
        'Kỳ quyết toán
        myDataTable.Rows(6).Item("GiaTri1") = BaseDB.ExecSql_DataValue("select tenkyqt from tblKyQT where id =" + dr.Item("KYQT").ToString()).ToString()

        'Người viết phiếu
        myDataTable.Rows(7).Item("GiaTri") = dr.Item("NGUOIVP")
        'Hạn phiếu
        Try
            myDataTable.Rows(7).Item("GiaTri1") = Format(dr.Item("NGAYPH"), FormatDateStyle())
        Catch ex As Exception
            myDataTable.Rows(7).Item("GiaTri1") = ""
        End Try

        'Ngày viết phiếu
        Try
            myDataTable.Rows(8).Item("GiaTri") = Format(dr.Item("NGAY"), FormatDateStyle())
        Catch ex As Exception
            myDataTable.Rows(8).Item("GiaTri") = ""
        End Try
    End Sub


    Private Shared Sub PopulateHeaderRowPhieuXuat(ByRef myDataTable As DataTable)
        AddDataRow(myDataTable, "Số phiếu", "Giấy giới thiệu")
        AddDataRow(myDataTable, "Đơn vị nhận", "Ngày ký GGT")
        AddDataRow(myDataTable, "Tính chất xuất", "Số khoản")
        AddDataRow(myDataTable, "Loại kho", "Số kiện")
        AddDataRow(myDataTable, "Theo chỉ lệnh", "Trọng lượng(kg)")
        AddDataRow(myDataTable, "Người giao", "HT thanh toán")
        AddDataRow(myDataTable, "Ngày giao", "Tổng giá trị")
        AddDataRow(myDataTable, "Người viết phiếu", "Kỳ quyết toán")
        AddDataRow(myDataTable, "Ngày viết phiếu", "Hạn phiếu")
    End Sub

    Private Shared Sub PopulateDataPhieuXuat(ByRef myDataTable As DataTable, ByVal dr As DataRowView)
        'Số phiếu
        myDataTable.Rows(0).Item("GiaTri") = dr.Item("SoPhieu")
        'Giấy giới thiệu
        myDataTable.Rows(0).Item("GiaTri1") = dr.Item("GGT")


        'Đơn vị nhận
        myDataTable.Rows(1).Item("GiaTri") = CDoiTuongs.GetNameByMa(enumDOITUONG.DONVI, dr.Item("DVGN"))
        'Ngày ký GGT
        Try
            myDataTable.Rows(1).Item("GiaTri1") = Format(dr.Item("NGAYKGGT"), FormatDateStyle())
        Catch ex As Exception
            myDataTable.Rows(1).Item("GiaTri1") = ""
        End Try


        'Tính chất xuất
        Try
            myDataTable.Rows(2).Item("GiaTri") = CDoiTuongs.GetNameByMa(enumDOITUONG.TINHCHATXUAT, dr.Item("TCNX"))
        Catch ex As Exception
            myDataTable.Rows(2).Item("GiaTri") = ""
        End Try
        'Số khoản
        myDataTable.Rows(2).Item("GiaTri1") = dr.Item("SOKH")

        'Loại kho
        myDataTable.Rows(3).Item("GiaTri") = CDoiTuongs.GetNameByMa(enumDOITUONG.LOAIKHO, dr.Item("LK"))
        'Số kiện
        myDataTable.Rows(3).Item("GiaTri1") = Format(dr.Item("SOKIEN"), FormatQuantityStyle())

        'Theo chỉ lệnh
        myDataTable.Rows(4).Item("GiaTri") = dr.Item("THEOLENH")
        'Trọng lượng
        myDataTable.Rows(4).Item("GiaTri1") = Format(dr.Item("TRLUONG"), FormatQuantityStyle())

        'Người giao
        myDataTable.Rows(5).Item("GiaTri") = dr.Item("NGUOIGN")
        'HT Thanh toán
        Try
            myDataTable.Rows(5).Item("GiaTri1") = CDoiTuongs.GetNameByMa(enumDOITUONG.THANHTOAN, dr.Item("HTTT"))
        Catch ex As Exception
            myDataTable.Rows(5).Item("GiaTri1") = ""
        End Try

        'Ngày giao
        Try
            myDataTable.Rows(6).Item("GiaTri") = Format(dr.Item("NGAYGN"), FormatDateStyle())
        Catch ex As Exception
            myDataTable.Rows(6).Item("GiaTri") = ""
        End Try
        'Tổng giá trị
        Try
            myDataTable.Rows(6).Item("GiaTri1") = CType(dr.Item("TGIATRI"), Double).ToString(FormatMoneyStyle())
        Catch ex As Exception
            myDataTable.Rows(6).Item("GiaTri1") = ""
        End Try

        'Người viết phiếu
        myDataTable.Rows(7).Item("GiaTri") = dr.Item("NGUOIVP")
        'Kỳ quyết toán
        myDataTable.Rows(7).Item("GiaTri1") = BaseDB.ExecSql_DataValue("select tenkyqt from tblKyQT where id =" + dr.Item("KYQT").ToString()).ToString()

        'Ngày viết phiếu
        Try
            myDataTable.Rows(8).Item("GiaTri") = Format(dr.Item("NGAY"), FormatDateStyle())
        Catch ex As Exception
            myDataTable.Rows(8).Item("GiaTri") = ""
        End Try

        'Hạn phiếu
        Try
            myDataTable.Rows(8).Item("GiaTri1") = Format(dr.Item("NGAYPH"), FormatDateStyle())
        Catch ex As Exception
            myDataTable.Rows(8).Item("GiaTri1") = ""
        End Try
    End Sub

    Public Shared Sub CreateDataSetDPNX(ByRef dsDataSet As DataSet)
        Try
            ' Declare variables for DataColumn and DataRow objects.
            Dim myDataColumn As DataColumn

            'Create Datatable phieu nhap
            Dim myDTPhieuNhap As DataTable = New DataTable("PhieuNhapInfo")
            myDataColumn = New DataColumn
            myDataColumn.DataType = System.Type.GetType("System.String")
            myDataColumn.ColumnName = "ThongSo"
            myDTPhieuNhap.Columns.Add(myDataColumn)

            myDataColumn = New DataColumn
            myDataColumn.DataType = System.Type.GetType("System.String")
            myDataColumn.ColumnName = "GiaTri"
            myDataColumn.Unique = False
            myDataColumn.AllowDBNull = True
            myDTPhieuNhap.Columns.Add(myDataColumn)

            myDataColumn = New DataColumn
            myDataColumn.DataType = System.Type.GetType("System.String")
            myDataColumn.ColumnName = "ThongSo1"
            myDTPhieuNhap.Columns.Add(myDataColumn)

            myDataColumn = New DataColumn
            myDataColumn.DataType = System.Type.GetType("System.String")
            myDataColumn.ColumnName = "GiaTri1"
            myDataColumn.AllowDBNull = True
            myDTPhieuNhap.Columns.Add(myDataColumn)

            'Create Datatable phieu xuat
            Dim myDTPhieuXuat As DataTable = New DataTable("PhieuXuatInfo")
            myDataColumn = New DataColumn
            myDataColumn.DataType = System.Type.GetType("System.String")
            myDataColumn.ColumnName = "ThongSo"
            myDTPhieuXuat.Columns.Add(myDataColumn)

            myDataColumn = New DataColumn
            myDataColumn.DataType = System.Type.GetType("System.String")
            myDataColumn.ColumnName = "GiaTri"
            myDataColumn.Unique = False
            myDataColumn.AllowDBNull = True
            myDTPhieuXuat.Columns.Add(myDataColumn)

            myDataColumn = New DataColumn
            myDataColumn.DataType = System.Type.GetType("System.String")
            myDataColumn.ColumnName = "ThongSo1"
            myDTPhieuXuat.Columns.Add(myDataColumn)

            myDataColumn = New DataColumn
            myDataColumn.DataType = System.Type.GetType("System.String")
            myDataColumn.ColumnName = "GiaTri1"
            myDataColumn.AllowDBNull = True
            myDTPhieuXuat.Columns.Add(myDataColumn)


            Dim strSQL As String = "select * from tblDPNX"
            dsDataSet = BaseDB.ExecSql_DataSet(strSQL)
            dsDataSet.Tables(0).TableName = "PhieuNX"

            PopulateHeaderRowPhieuNhap(myDTPhieuNhap)
            dsDataSet.Tables.Add(myDTPhieuNhap)

            PopulateHeaderRowPhieuXuat(myDTPhieuXuat)
            dsDataSet.Tables.Add(myDTPhieuXuat)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Shared Sub FillDataSetDPNX(ByVal pDPNX_ID As Integer, ByRef bsDPNX As BindingSource, ByRef dsDataSet As DataSet)
        Try
            bsDPNX.Filter = "dpnx_id = " + pDPNX_ID.ToString()
            Dim dr As DataRowView = bsDPNX.Item(0)

            If dr.Item("NX") = "N" Then
                PopulateDataPhieuNhap(dsDataSet.Tables("PhieuNhapInfo"), dr)
            Else
                PopulateDataPhieuXuat(dsDataSet.Tables("PhieuXuatInfo"), dr)
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class
